﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using GameEngine.Clients.Silverlight.View;
using MySettlers.Screens;
using GameEngine.Model.Data;
using MySettlers.View.Components.editor;
using GameEngine.Clients.Silverlight;
using GameEngine;

namespace MySettlers.Commands
{
    public class Page_EditorNewGame_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var worldVO = new WorldVO<int>(new WorldVOId("myWorld")) { 
                Map = new MapVO<int>("uniMap")
            };
            worldVO.Map.CreateTiles(new Point(50, 50));
            var levelVO = new LevelVO<int>(new LevelVOId("lvl0"))
            {
                
            };
            levelVO.Worlds.Add(worldVO);
            var gameVO = new GameVO<int>(new GameVOId("0"))
            {
                CurrentLevel = 0,
            };
            gameVO.Levels.Add(levelVO);
            gameVO.LevelOrder.Add(0, levelVO.Id);

            MyFacade.Instance.AddGameDefaultData(gameVO);

            var gameDataProxy = SilverlightApplicationFacadeHelper.CreateGame(gameVO);
            var levelDataProxy = SilverlightApplicationFacadeHelper.CreateLevel(levelVO);
            var worldDataProxy = SilverlightModelFacade.Helper.GetWorldDataProxy(worldVO.Id);

            var screen = new EditorPreSettingsUC();
            var mediator = MyViewFacade.Instance.CreateMenuMediator<string, EditorPreSettingsUC>(
                MyFacade.EditorPreSettingsPageMediatorName, 
                screen, 
                MyFacade.PAGE_EDITORNEWGAME_OUT_COMMAND);
            MyFacade.Instance.RegisterMediator(mediator);

            screen.GameName = gameDataProxy.Name;
            screen.MapWidth = worldDataProxy.Map.Size.X.ToString();
            screen.MapHeight = worldDataProxy.Map.Size.Y.ToString();
            MyFacadeHelper.DesktopNavigate(mediator.ViewComponent);
        }
    }

    public class Page_EditorNewGame_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var mediator = MyFacade.Instance.RetrieveMediator(MyFacade.EditorPreSettingsPageMediatorName);
            var screen = mediator.ViewComponent as EditorPreSettingsUC;

            var game = SilverlightModelFacade.Instance.Game;
            var lvl = game.CurrentLevel;
            var worlds = lvl.Worlds.GetEnumerator();
            worlds.MoveNext();
            var world = worlds.Current.Value;
            
            switch (notification.Body as string)
            {
                case EditorPreSettingsUC.BACKBUTTON:
                    SilverlightApplicationFacadeHelper.DestroyGame();
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_EDITORMAINMENU_IN_COMMAND);
                    break;
                case EditorPreSettingsUC.NEXTBUTTON:
                    game.Name = screen.GameName;
                    world.Map.CreateTiles(new Point(double.Parse(screen.MapWidth), double.Parse(screen.MapHeight)));
                    //TODO set teams
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_EDITOREDITGAME_IN_COMMAND);
                    break;
            }
            MyFacade.Instance.RemoveMediator(MyFacade.EditorPreSettingsPageMediatorName);
        }
    }
}
